# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: Adrià Cabello <adro.cc79 at protonmail dot com>
# Contributor: Fabio Loli <fabio.loli@disroot.org>

pkgname=materialx
pkgver=1.38.8
pkgrel=7
pkgdesc="Open standard for representing rich material and look-development content in computer graphics"
arch=('loong64' 'x86_64')
url="https://materialx.org/"
license=('Apache')
depends=('glibc' 'gcc-libs' 'glfw' 'libglvnd' 'libx11' 'libxt' 'python'
         'python-setuptools' 'opencolorio' 'dos2unix' 'zenity')
makedepends=('cmake' 'chrpath' 'libxinerama' 'libxcursor' 'pybind11' 'ninja')
source=("https://github.com/AcademySoftwareFoundation/MaterialX/releases/download/v${pkgver}/MaterialX-${pkgver}.tar.gz"
        "materialx-grapheditor.desktop"
        "materialx-view.desktop"
        "materialx.xml")
sha256sums=('6769800cc3c15a9ecc99933774824ed5a766382f71966ab607c22ca33a4d0162'
            '88e5ecafa8088b90f799b49c36af59f8462ca7426cdec58215332ee283556ddb'
            '2f2b675540fea39a749f89083a9c341319c1f7b478fbb049a77bd66c29b2ee01'
            'd9b9426fb94121da052b796542cc74a0c5d7cef06997be70611c25f345553861')

_pyver=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')

prepare() {
  cd MaterialX-${pkgver}

  # Repath
  sed -i 's|CMAKE_INSTALL_PREFIX|CMAKE_BINARY_DIR|g' python/CMakeLists.txt

  sed -i 's|resources|/usr/share/materialx/resources|g' source/MaterialXView/{Main.cpp,Viewer.cpp}
  sed -i 's|"libraries"|"/usr/share/materialx/libraries"|g' source/MaterialXView/Main.cpp
  sed -i 's|resources|/usr/share/materialx/resources|g' source/MaterialXGraphEditor/{Main.cpp,Graph.cpp}
  sed -i 's|"libraries"|"/usr/share/materialx/libraries"|g' source/MaterialXGraphEditor/{Main.cpp,Graph.cpp}
  sed -i 's|"libraries"|"/usr/share/materialx/libraries"|g' source/MaterialXGenShader/GenOptions.h

  dos2unix python/Scripts/*
}

build() {
  cmake -S MaterialX-${pkgver} -B build -G Ninja \
  	-DCMAKE_INSTALL_PREFIX=/usr \
  	-DCMAKE_SKIP_INSTALL_RPATH=ON \
  	-DCMAKE_SKIP_RPATH=ON \
  	-Wno-dev \
  	-DMATERIALX_BUILD_SHARED_LIBS=ON \
  	-DMATERIALX_BUILD_PYTHON=ON \
  	-DMATERIALX_BUILD_VIEWER=ON \
  	-DMATERIALX_BUILD_GRAPH_EDITOR=ON

  cmake --build build
}

package() {
  DESTDIR=${pkgdir} cmake --install build

  find ${pkgdir} -type f -name "README.md" -exec rm {} \;
  rm ${pkgdir}/usr/CHANGELOG.md

  for file in ${pkgdir}/usr/python/Scripts/*; do
  	name="${file%.py}"
  	chmod +x "$file"
  	mv "$file" $name
  done

  mkdir -p ${pkgdir}/usr/share/$pkgname
  mv ${pkgdir}/usr/resources ${pkgdir}/usr/share/$pkgname/resources
  mv ${pkgdir}/usr/libraries ${pkgdir}/usr/share/$pkgname/libraries

  cp ${pkgdir}/usr/python/Scripts/* ${pkgdir}/usr/bin
  rm -r ${pkgdir}/usr/python/Scripts

  mkdir -p ${pkgdir}/usr/lib/python$_pyver
  mv ${pkgdir}/usr/python $_/site-packages

  install -Dm755 ${srcdir}/MaterialX-${pkgver}/documents/Images/MaterialXLogo_200x155.png ${pkgdir}/usr/share/icons/hicolor/256x256/apps/materialx.png

  # Fix CMake configs
  sed -i 's|libraries|share/materialx/libraries|g' ${pkgdir}/usr/lib/cmake/MaterialX/MaterialXConfig.cmake
  sed -i 's|python|lib/python'$_pyver'/site-packages/MaterialX|g' ${pkgdir}/usr/lib/cmake/MaterialX/MaterialXConfig.cmake
  sed -i 's|resources|share/materialx/resources|g' ${pkgdir}/usr/lib/cmake/MaterialX/MaterialXConfig.cmake

  mkdir -p ${pkgdir}/usr/share/{applications,mime/model,licenses/$pkgname}
  cp ${srcdir}/{materialx-grapheditor.desktop,materialx-view.desktop} ${pkgdir}/usr/share/applications
  install -Dm644 ${srcdir}/materialx.xml ${pkgdir}/usr/share/mime/model/materialx.xml
  mv ${pkgdir}/usr/{LICENSE,THIRD-PARTY.md} ${pkgdir}/usr/share/licenses/materialx/

  # Remove junk
  rm -rf ${pkgdir}/build

  # Remove empty dirs
  find ${pkgdir}/usr -empty -type d -delete
}
